<?php
/**
 * Created by PhpStorm.
 * User: 宋春宝
 * Date: 2018/9/11
 * Time: 15:49
 */

namespace app\home\controller;


use think\Controller;
use think\Request;

class Works extends Common
{

    public function initialize()
    {
        parent::initialize();
    }

    /**
     * 创建工单
     * @param Request $request
     * @return array
     */
    public function add(Request $request){
        $input = $request->param();
        $msg = $this->validate($input,'app\admin\validate\Dispatch');
        if($msg!='true'){
            return json(['code'=>0,'msg'=>$msg]);
        }
        $input['state_id'] = 10;
        $input['number'] = date('YmdHis').rand(100,999);
        $row = db('works')->insertGetId($input);
        if($row)
            return json(['code'=>1,'msg'=>'提交成功，你的工单号码为'.$input['number'].'']);
        return json(['code'=>0,'msg'=>'提交失败']);
    }

    /**
     * 查询工单状态
     * @param Request $request
     * @return array
     */
    public function query(Request $request){
        $no = $request->param('no');
        $pol = substr($no,0,1);
        if($pol=='D'){
            $info = db('dispatch')
                ->where('number',$no)
                ->find();

            $rows = db('dispatch_sheets')->alias('ds')
                ->where('ds.dispatch_id',$info['id'])
                ->join('admin ad','ds.admin_id=ad.admin_id','left')
                ->field(['ds.*','ad.name as admin_name'])->order('ds.id','desc')->select();
            foreach ($rows as &$v){
                $v['list'] = db('dispatch_flow')->alias('df')
                    ->where('df.sheet_id',$v['id'])
                    ->join('admin ad','ad.admin_id=df.admin_id','left')
                    ->field(['df.*','ad.name as admin_name'])->order('id','desc')->select();
                foreach ($v['list'] as &$vl){
                    $vl['attach'] = explode(';',$vl['attach']);
                    $vl['attach'] = array_filter($vl['attach']);
                }
            }

            return ['code'=>1,'msg'=>'','data'=>$rows];
        }else{
            if(!is_numeric($no)) return ['code'=>0,'msg'=>'参数错误'];
            $info = db('works')->alias('w')
                ->where('w.number',$no)
                ->join('state st','st.id=w.state_id','left')
                ->join('dispatch dp','dp.works_id=w.id','left')
                ->field(['w.state_id','st.state_name','w.id','dp.id as dispatch_id'])
                ->find();
            if($info['state_id']!=12&&$info['state_id']!=13){
                $rows = db('dispatch_sheets')->alias('ds')
                    ->where('ds.dispatch_id',$info['dispatch_id'])
                    ->join('admin ad','ds.admin_id=ad.admin_id','left')
                    ->field(['ds.*','ad.name as admin_name'])->order('ds.id','desc')->select();
                foreach ($rows as &$v){
                    $v['list'] = db('dispatch_flow')->alias('df')
                        ->where('df.sheet_id',$v['id'])
                        ->join('admin ad','ad.admin_id=df.admin_id','left')
                        ->field(['df.*','ad.name as admin_name'])->order('id','desc')->select();
                    foreach ($v['list'] as &$vl){
                        $vl['attach'] = explode(';',$vl['attach']);
                        $vl['attach'] = array_filter($vl['attach']);
                    }
                }
                return ['code'=>1,'msg'=>'','data'=>$rows];
            }
            if($info['state_id']==14) return ['code'=>0,'msg'=>'该工单已关闭'];
        }
        return ['code'=>0,'msg'=>$info['state_name'],'data'=>''];
    }

    //查询
    public function search_no(Request $request)
    {
        if ($request->param('no')){
            $list = $this->query($request);
            $this->assign('list',$list);
        }
        return $this->fetch();
    }

    /**
     * 展示工单列表
     */
    public function workList(){
        $per_page = empty(input('per_page'))?10:input('per_page');
        $all = db('dispatch')->where(['is_show'=>1])->order('id','desc')
            ->field(['number','c_time','state_id','title','desc'])
            ->paginate($per_page);
        return $all;
    }
}